#!/usr/bin/env python
# -*- coding:utf-8 -*-

import base64
import requests
import os, time, json, sys
import importlib

import http.client as httplib

from concurrent.futures import ProcessPoolExecutor
from concurrent.futures import ThreadPoolExecutor
importlib.reload(sys)

File_List = []

Path = '/Users/shilei/Desktop/jianli.2'
file_path = '/Users/shilei/Desktop/gui1.pdf'
username='u100049'
pwd = 'test20180621'
url = 'http://service.ygys.net/resumeservice.asmx?WSDL'

pool = ProcessPoolExecutor(5)

def upload_file(file_path):

    fileContent = open(file_path, 'rb').read()
    base64Str = base64.b64encode(fileContent).decode("utf-8")
    suffix = os.path.splitext(os.path.basename(file_path))[1]

    Data = '<?xml version="1.0" encoding="utf-8"?>'
    Data += '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
    Data += '<soap:Body>'
    Data += '<TransResumeByJsonStringForFileBase64 xmlns="http://tempuri.org/">'  # Interface name
    Data += '<username>u100049</username>'
    # username
    Data += '<pwd>test20180621</pwd>'
    Data += '<content>' + base64Str + '</content>'  # The base64 string of the file
    Data += '<ext>' + suffix + '</ext>'
    # Extension name
    Data += '</TransResumeByJsonStringForFileBase64>'
    Data += '</soap:Body>'
    Data += '</soap:Envelope>'

    url = 'http://service.ygys.net/resumeservice.asmx?WSDL'
    headerdata = {'SOAPAction': 'http://tempuri.org/TransResumeByJsonStringForFileBase64',
                  'Content-Type': 'text/xml; charset=utf-8'}

    conn = httplib.HTTPConnection("service.ygys.net", 80)
    conn.request('POST', url, Data, headerdata)
    response = conn.getresponse()

    result = response.read()

    selector = html.fromstring(result)
    text = selector.xpath("//*/*/text()")

    fileName = os.path.join(os.path.basename(os.path.dirname(file_path)), os.path.basename(file_path))
    return write_file(fileName, text)

def write_file(fileName, text):
    return fileName, text